﻿<?xml version="1.0" encoding="utf-8" ?>
<document name="Test">
  <function name="functionTest"
            parameters="x,y">
    <![CDATA[
    if(x == null) x = 1;
    if(y == null) y = 1;
    return x + y * 2;
    ]]>
  </function>
  <xsql name="comm_student">
    SELECT ST.STUDENT_ID,
    ST.STUDENT_NAME,
    ST.STUDENT_SEX,
    ST.STUDENT_BIRTHDAY,
    ST.STUDENT_PHOTO,
    ST.CLASS_ID,
    ST.PLACE_ID
    FROM STUDENT_TBL ST
  </xsql>
  <xsql name="SelectStudentByAll">
    <include path="comm_student" />
    <trim prefix="WHERE"
          trim="AND|OR">
      <if test="studentSex != null and studentSex != '' ">
        AND ST.STUDENT_SEX IN (#{studentName, string[,]});

      </if>
      <else>

      </else>
      <if test="IsNotEmpty(studentName)">
        ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')
      </if>
      <if test="studentSex != null and studentSex != '' ">
        AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}
      </if>
      <if test="studentBirthday != null ">
        AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}
      </if>
      <if test="classId != null and classId!= '' ">
        AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}
      </if>
      <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">
        AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}
      </if>
      <if test="placeId != null and placeId != '' ">
        AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}
      </if>
      <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">
        AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}
      </if>
      <if test="studentId != null and studentId != '' ">
        AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}
      </if>
    </trim>
  </xsql>

  <xsql name="if_else_XML">
    SELECT * FROM STUDENTS
    <trim prefix="WHERE"
          trim="-">
      <if test="sutdentID != null">

        -AND STUDENTID=#{studentID,int}
        AND STUDENTID=#{sutdentName,string}
        AND star AND
        AND S OR
        AND OR-
      </if>
      <else>
        ORDER BY STUDENTID ASC
      </else>
    </trim>
  </xsql>

  <xsql name="switch_XML">
    SELECT * FROM STUDENTS
    <trim prefix="WHERE"
          trim="AND|OR">
      <switch test="studentID">
        AND
        <case test="==null">
          STUDENTID IS NULL
        </case>
        <case test="==''">
          len(STUDENTID)=1
        </case>
        <case test="==1">
          STUDENTID IN (1,2,3)
        </case>
        <defualt>
          STUDENTID IN (4,5,6)
        </defualt>
      </switch>
    </trim>
  </xsql>
  <xsql name="foreach_XML">
    SELECT * FROM STUDENTS
    <trim prefix="WHERE"
          trim="AND|OR">
      <if test="sutdentName != null">
        AND STUDENTID=#{sutdentName,string}
        AND star AND
        AND S OR
        AND OR
      </if>
      <if>
        <test>
          <![CDATA[studentNames != null && studentNames.Length>0]]>
        </test>
        AND STUDENTNAME IN(
        <!-- 例子-->
        <foreach var="studentName2"
                 in="studentNames"
                 index="index"
                 trim=",">
          ,#{studentName2} || #{index,string}
          <call function="functionTest"
                args="studentName2"/>
        </foreach>
        )
      </if>
    </trim>
  </xsql>
  <xsql name="update_XML">
    #{tableName,none}
    UPDATE STUDENTS
    <trim prefix="SET"
          trim=",">
      <if test="studentName != null">
        ,STUDENTNAME=#{studentName,string}
      </if>
      <if test="age != null">
        ,AGE=#{studentName,string}
      </if>
    </trim>
  </xsql>
  <xsql name="insert_XML">
    <parameters>
      <xsql name="student_id"
            cache="forever">select studentPKSequence.nextVal from dual</xsql>
      <expression name="student_id3">student_id+1</expression>
    </parameters>
    INSERT INTO STUDENTS(STUDENT_ID,STUDENT_NAME) VALUES(#{student_id,decimal},#{name,string})
  </xsql>
</document>